package com.wfk.mac.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wfk.utils.BasePage;
import com.wfk.utils.Login;
import com.wfk.utils.ResponseData;
import com.wfk.utils.TokenUtils;
import com.wfk.utils.ValidateUtil;
import com.wfk.entity.Account;
import com.wfk.entity.MoneyDetails;
import com.wfk.entity.SysCashDetails;
import com.wfk.enums.HandleType;
import com.wfk.h5.service.FrontService;
import com.wfk.h5.service.MemberService;
import com.wfk.jwt.JWT;
import com.wfk.mac.mapping.MappingUtils;
import com.wfk.service.IAccountService;
import com.wfk.service.IAppBannersService;
import com.wfk.service.IBankDetailsService;
import com.wfk.service.IBankTypeService;
import com.wfk.service.IBaseInfoService;
import com.wfk.service.IMoneyDetailsService;
import com.wfk.service.ISysCashDetailsService;
import com.wfk.service.ISysNewsService;
import com.wfk.service.ISysRoleService;
import com.wfk.service.ISysRuleService;

@Controller
public class UserController {
	
	@Autowired
	private FrontService frontService;
	@Autowired
	private IMoneyDetailsService moneyDetailsService;
	@Autowired
	private IAccountService accountService;
	@Autowired
	private IAppBannersService  appBannersService;
	@Autowired
	private ISysNewsService sysNewsService;
	@Autowired
	private IBankTypeService bankTypeService;
	@Autowired
	private IBankDetailsService bankDetailsService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private ISysCashDetailsService sysCashDetailsService;
	@Autowired
	private ISysRuleService sysRuleService;
	@Autowired
	private IBaseInfoService baseInfoService;
	
	@RequestMapping(MappingUtils.updateToken)
	@ResponseBody
	public ResponseData updateToken(HttpServletRequest request) {
		try {
			String token = TokenUtils.getToken(request) ;
			//token不存在
			if(null != token) {
				Login login = JWT.unsign(token, Login.class);
				if(null!=login) {
					token = JWT.sign(login,TimeUnit.DAYS.toMillis(1)) ;//加密1天
					return ResponseData.ok().putDataValue("token", token);
				}
			}
			return ResponseData.baseError("登录过期");
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseData.baseServiceError("内部错误");
		}
	}
	
	@RequestMapping(MappingUtils.newDetail)
	@ResponseBody
	public ResponseData newDetail(Integer id) {
		try {
			return ResponseData.ok().putDataValue("result", this.sysNewsService.getById(id));
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接，请重试");
		}
	}
	
	@RequestMapping(MappingUtils.sysRule)
	@ResponseBody
	public ResponseData sysRule(String title) {
		try {
			return ResponseData.ok().putDataValue("result", this.sysRuleService.getTitle(title));
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接，请重试");
		}
	}
	@RequestMapping(MappingUtils.handleCash)
	@ResponseBody
	public ResponseData doCash(Integer bankid,BigDecimal cashmoney,Integer cid,String payword) {
		try {
			SysCashDetails cashDetail =  new SysCashDetails();
			cashDetail.setBankid(bankid);
			cashDetail.setCashmoney(cashmoney);
			cashDetail.setTargetid(cid);
			cashDetail.setPayword(payword);
			return sysCashDetailsService.doCash(cashDetail);
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接，请重试");
		}
	}
	
	
	@RequestMapping(MappingUtils.cashRule)
	@ResponseBody
	public ResponseData cashRule(Integer cid) {
		try {
			return memberService.cashRule(cid);
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接，请重试");
		}
	}
	
	@RequestMapping(MappingUtils.isPayword)
	@ResponseBody
	public ResponseData isPayword(Integer cid){
		try {
			return ResponseData.ok().putDataValue("result", this.memberService.isPayword(cid));
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseData.badRequest();
		}
	}
	@RequestMapping(MappingUtils.isBankDetail)
	@ResponseBody
	public ResponseData isBankDetail(Integer cid){
		try {
			return ResponseData.ok().putDataValue("result", this.memberService.isBankDetail(cid));
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseData.badRequest();
		}
	}
	/**
	 * 
	 * 银行卡类型列表
	 * @return
	 */
	@RequestMapping(MappingUtils.bankTypeList)
	@ResponseBody
	public ResponseData bankTypes(){
		try {
			return ResponseData.ok().putDataValue("result", this.bankTypeService.list(null));
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseData.baseServiceError("内部错误");
		}
	}
	
	/**
	 * 
	 * 绑定银行卡
	 * @param cid
	 * @param realname
	 * @param idcard
	 * @param bankcode
	 * @param typeid
	 * @param phone
	 * @return
	 */
	@RequestMapping(MappingUtils.bindBank)
	@ResponseBody
	public ResponseData saveBank(Integer cid,String realname,String idcard,String bankcode,Integer typeid,String phone){
		try{
			return this.memberService.saveBank(cid, realname, idcard, bankcode, typeid, phone);
		}catch(Exception e){
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接错误，请重试");
		}
	}
	
	/**
	 * 我的银行卡列表
	 * @param cid
	 * @return
	 */
	@RequestMapping(MappingUtils.bindBankDev)
	@ResponseBody
	public ResponseData bindBankDev(Integer id){
		try{
			this.bankDetailsService.removeById(id);
			return ResponseData.ok();
		}catch(Exception e){
			return ResponseData.baseServiceError("网络连接错误，请重试");
		}
	}
	
	/**
	 * 我的银行卡列表
	 * @param cid
	 * @return
	 */
	@RequestMapping(MappingUtils.mybankdetail)
	@ResponseBody
	public ResponseData myBankDetail(Integer cid){
		try{
			return memberService.cashRule(cid);
		}catch(Exception e){
			return ResponseData.baseServiceError("网络连接错误，请重试");
		}
	}
	
	
	@RequestMapping(MappingUtils.home)
	@ResponseBody
	public ResponseData home(Integer cid) {
		try {
			ResponseData result = ResponseData.ok();
			Account account = this.accountService.get(cid);
			result.putDataValue("banner",appBannersService.list(null));
			result.putDataValue("news", sysNewsService.list(null));
			result.putDataValue("dayMoney",account==null?0:account.getDayMoney());
			result.putDataValue("allMoney", account==null?0:account.getAllMoney());
			return result;
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接，请重试");
		}
	}
	
	@RequestMapping(MappingUtils.login)
	@ResponseBody
	public ResponseData login(String phone,String password) {
		try {
			return this.frontService.macLogin(phone, password);
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseData.baseServiceError("内部错误");
		}
	} 
	
	/**
	 * 余额明细
	 * @param cid
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(MappingUtils.moneydetail)
	@ResponseBody
	public ResponseData showMoneyDetail(MoneyDetails moneyDetails,BasePage<MoneyDetails> page){
		try{
			ResponseData result = ResponseData.ok();
			if(moneyDetails.getCid()==null) {
				return ResponseData.baseServiceError("会员未注册");
			}
			moneyDetails.setTypeid(HandleType.brush.getId());
			this.moneyDetailsService.listPage(moneyDetails,page);
			result.putDataValue("result",page);
			result.putDataValue("money",this.accountService.get(moneyDetails.getCid()).getMoney());
			return result;
		}catch(Exception e){
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接错误，请重试");
		}
	}
	
	@RequestMapping(MappingUtils.getBase)
	@ResponseBody
	public ResponseData getBase(String key) {
		try {
			if(!ValidateUtil.equalTo("xinshou", key)&&!ValidateUtil.equalTo("shouyi",key)) {
				return ResponseData.baseError("");
			}
			return ResponseData.ok().putDataValue("result", this.baseInfoService.getByKey(key));
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接，请重试");
		}
	}
}
